package com.lin.http;

import java.io.*;
import java.net.URLDecoder;
import java.util.*;

public class DataConvert {
	public static void main(String[] args) throws IOException {
		convert1();
		//convert2();
	}

	public static void convert1() throws IOException {
		BufferedReader bufferedReader = new BufferedReader(new FileReader("d://data.txt"));
		PrintWriter printWriter = new PrintWriter("d://out.txt");
		String line = bufferedReader.readLine();
		String[] datas = line.split("&");
		List<String> dataList = new ArrayList<>(Arrays.asList(datas));
		dataList.sort(String.CASE_INSENSITIVE_ORDER);
		for (String data : dataList) {
			String gbk = URLDecoder.decode(data, "GBK");
			System.out.println(gbk);
			printWriter.println(gbk);
		}
		printWriter.flush();
	}

	public static void convert2() throws IOException {
		BufferedReader bufferedReader = new BufferedReader(new FileReader("d://data.txt"));
		PrintWriter printWriter = new PrintWriter("d://out.txt");
		String line = bufferedReader.readLine();
		String[] datas = line.split("&");
		List<String> dataList = new ArrayList<>(Arrays.asList(datas));
		Map<String, String> map = new LinkedHashMap<>();

		dataList.sort(String.CASE_INSENSITIVE_ORDER);
		for (String data : dataList) {
			String gbk = URLDecoder.decode(data, "GBk");
			String[] split = gbk.split("=");
			if (split.length < 2)
				map.put(split[0], "");
			else
				map.put(split[0], split[1]);
		}
		map.forEach((key, value)->{
			printWriter.println(key + "=" + value);
			System.out.println(key + "=" + value);
		});

		printWriter.flush();
	}
}
